var app = getApp();
Page({
  data: {
    painting: {},
    shareImage: '',
    detail: '',
    userInfo: '',
    id: '',
    imgurl: ''
  },
  onLoad(options) {
    var _this = this;
    var adviser_id = options.adviser_id;
    var _code = options.code ? decodeURIComponent(options.code) : null;
    console.log(_code)
    //  var userinfomation = wx.getStorageSync('user_info')
    console.log(adviser_id )
    _this.setData({
      adviser_id: adviser_id,
    })
    var openid = wx.getStorageSync('user_new');
    if (!openid) {
      wx.showModal({
        title: '提示',
        content: '本功能需要授权后才可使用，点击确定跳转到登录界面',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/member/index',
              });
            }, 500)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return;
    }
    app.core('get', 'getAllProjectByAdviserIdAndCode', { adviser_id: adviser_id, flag: 2 }, (res) => {
      console.log(res,55);

      _this.setData({
        adviserInfo: res.adviserInfo
      })
      var url = res.data[0].buildNoPhoto;
      var id = _this.data.adviser_id;
      var code = _code ? _code : res.adviserInfo.code;
      var imgU = url.replace('http', 'https')
      var imgUrl = imgU.replace('com', 'cn')

      var houses =  res.data;
      console.log(houses, 123)
      var projectNames = '';
      for (var i = 0; i < houses.length ;i++){
        console.log(houses[i],123)
        projectNames += houses[i].projectName + ','
      }

      console.log(imgUrl,1122)
      console.log(code, 222333)
      _this.setData({
        imgurl: imgUrl,
        code: code,
        projectNames: projectNames
      })

      _this.eventDraw()
    })
    // setTimeout(() => {

    // }, 1000)
  },
  onShow() {
  },
  eventDraw() {
    wx.showLoading({
      title: '绘制海报中...',
      mask: true
    })
    var userinfomation = wx.getStorageSync('userInfo');
    var imgurl = this.data.imgurl;
    var code = this.data.code;
    console.log(imgurl)
    // return;
    var adviserInfo = this.data.adviserInfo;
    var projectNames = this.data.projectNames
    console.log(adviserInfo,66666)
    this.setData({
      painting: {
        width: 375,
        height: 555,
        clear: true,
        views: [
          {
            type: 'image',
            url: 'https://xkcloude.hfhouse.com/storage/advert/2019-09-03/6ePMy2tJffkU6XwA6y7GdIe05MISj1lT3p0C1DFm.png',
            top: 0,
            left: 0,
            width: 375,
            height: 555
          },
          {
            type: 'image',
            url: userinfomation.avatarUrl,
            top: 27.5,
            left: 29,
            width: 55,
            height: 55
          },
          {
            type: 'image',
            url: 'https://xkcloude.hfhouse.com/storage/advert/2019-09-04/GtLogXoFwh5lpetAlbd1Dkd7YcZ74B4YCuz2K2lW.png',
            top: 27.5,
            left: 29,
            width: 55,
            height: 55
          },
          {
            type: 'text',
            content: userinfomation.nickName + '   在星房惠小程序',
            fontSize: 16,
            color: '#FBFBFB',
            textAlign: 'left',
            top: 33,
            left: 96,
            bolder: true
          },
          {
            type: 'text',
            content: '发现一处好楼盘，快来看看',
            fontSize: 16,
            color: '#FBFBFB',
            textAlign: 'left',
            top: 59.5,
            left: 96
          },
          {
            type: 'image',
            url: imgurl,
            top: 116,
            left: 42.5,
            width: 290,
            height: 186
          },
          {
            type: 'image',
            url: code,
            top: 433,
            left: 55,
            width: 82,
            height: 82
          },
          {
            type: 'text',
            content: projectNames,
            fontSize: 20,
            lineHeight: 21,
            color: '#2665EC',
            textAlign: 'left',
            top: 316,
            left: 44,
            width: 287,
            MaxLineNumber: 2,
            breakWord: true,
            bolder: true
          },
          {
            type: 'text',
            content: "网上优质楼盘在家购 安心放心随時聊",
            fontSize: 16,
            lineHeight: 21,
            color: '#383549',
            textAlign: 'left',
            top: 374,
            left: 44,
            width: 287,
            MaxLineNumber: 2,
            breakWord: true,
            bolder: true
          },
          // {
          //   type: 'text',
          //   content: '1',
          //   fontSize: 20,
          //   color: '#E62004',
          //   textAlign: 'left',
          //   top: 385,
          //   left: 44,
          //   bolder: true
          // },
          // {
          //   type: 'text',
          //   content: '6699',
          //   fontSize: 13,
          //   color: '#383549',
          //   textAlign: 'left',
          //   top: 388,
          //   left: 155,
          // },
          // {
          //   type: 'text',
          //   content: detail.junior_school.name,
          //   fontSize: 13,
          //   color: '#383549',
          //   textAlign: 'left',
          //   top: 390,
          //   left: 155,
          //   width: 180,
          //   breakWord: true,
          // },
          // {
          //   type: 'text',
          //   content: detail.middle_school.name,
          //   fontSize: 13,
          //   color: '#383549',
          //   textAlign: 'left',
          //   top: 410,
          //   left: 155,
          //   width: 180,
          //   breakWord: true,
          // },
          {
            type: 'text',
            content: '识别小程序二维码',
            fontSize: 14,
            color: '#383549',
            textAlign: 'left',
            top: 460,
            left: 165.5,
            lineHeight: 20,
            width: 125
          },
          {
            type: 'text',
            content: '进入星房惠查看楼盘',
            fontSize: 14,
            color: '#383549',
            textAlign: 'left',
            top: 480,
            left: 165.5,
            lineHeight: 20,
            width: 125
          }
        ]
      }
    })
  },
  eventSave() {
    var id = this.data.id;
    console.log(id)
    // console.log(this.data.shareImage)
    // let _this = this;
    wx.saveImageToPhotosAlbum({
      filePath: this.data.shareImage,
      success(res) {
        wx.showToast({
          title: '保存图片成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/member/index',
          });
        }, 1000)
      },
      fail(err) {
        if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
          // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
          wx.showModal({
            title: '提示',
            content: '需要您授权保存相册',
            showCancel: false,
            success: modalSuccess => {
              wx.openSetting({
                success(settingdata) {
                  console.log("settingdata", settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限成功,再次点击图片即可保存',
                      showCancel: false,
                    })
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限失败，将无法保存到相册哦~',
                      showCancel: false,
                    })
                  }
                },
                fail(failData) {
                  console.log("failData", failData)
                },
                complete(finishData) {
                  console.log("finishData", finishData)
                }
              })
            }
          })
        }
      },
    })
  },
  eventGetImage(event) {
    console.log(event)
    wx.hideLoading()
    const { tempFilePath, errMsg } = event.detail
    if (errMsg === 'canvasdrawer:ok') {
      this.setData({
        shareImage: tempFilePath
      })
    }
  },
  previewImg(e) {
    var current = e.target.dataset.src;
    console.log(current)
    wx.previewImage({
      current: current,
      urls: [this.data.shareImage]
    })
  },



  eventCodeImgSave(){
    var code = this.data.adviserInfo.code;
    wx.downloadFile({
      url: code, //仅为示例，并非真实的资源
      success(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
              wx.showToast({
                title: '保存成功',
              })
          },
          fail(res){

          }
        })
      }
    })

  }

  // share() {
  //   wx.updateShareMenu({
  //     withShareTicket: true,
  //     success(res) {
  //       console.log(res)
  //     }
  //   })
  // }
})
